#include "adddialog.h"

#include <QtWidgets>

//! [0]
AddDialog::AddDialog(QWidget *parent)
    : QDialog(parent),
      domainText(new QLineEdit),
      usernameText(new QLineEdit),
      passwordText(new QLineEdit),
      noteText(new QTextEdit)
{
    auto domainLabel = new QLabel(tr("Domain"));
    auto usernameLabel = new QLabel(tr("Username"));
    auto passwordLabel = new QLabel(tr("Password"));
    auto noteLabel = new QLabel(tr("Note"));
    auto okButton = new QPushButton(tr("OK"));
    auto cancelButton = new QPushButton(tr("Cancel"));

    auto gLayout = new QGridLayout;
    gLayout->setColumnStretch(1, 2);
    gLayout->addWidget(domainLabel, 0, 0);
    gLayout->addWidget(domainText, 0, 1);

    gLayout->setColumnStretch(1, 2);
    gLayout->addWidget(usernameLabel, 1, 0);
    gLayout->addWidget(usernameText, 1, 1);

    gLayout->setColumnStretch(1, 2);
    gLayout->addWidget(passwordLabel, 2, 0);
    gLayout->addWidget(passwordText, 2, 1);

    gLayout->addWidget(noteLabel, 3, 0, Qt::AlignLeft|Qt::AlignTop);
    gLayout->addWidget(noteText, 3, 1, Qt::AlignLeft);

    auto buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);

    gLayout->addLayout(buttonLayout, 4, 1, Qt::AlignRight);

    auto mainLayout = new QVBoxLayout;
    mainLayout->addLayout(gLayout);
    setLayout(mainLayout);

    connect(okButton, &QAbstractButton::clicked, this, &QDialog::accept);
    connect(cancelButton, &QAbstractButton::clicked, this, &QDialog::reject);

    setWindowTitle(tr("Add a Contact"));
}

QString AddDialog::domain() const
{
    return domainText->text();
}

QString AddDialog::username() const
{
    return usernameText->text();
}

QString AddDialog::password() const
{
    return passwordText->text();
}


QString AddDialog::note() const
{
    return noteText->toPlainText();
}

void AddDialog::editAddress(const QString &domain, const QString &username,
                            const QString &password, const QString &note)
{
    domainText->setReadOnly(true);
    domainText->setText(domain);
    usernameText->setText(username);
    passwordText->setText(password);
    noteText->setPlainText(note);
}
//! [0]
